/*
 * screw-core - 简洁好用的数据库表结构文档生成工具
 * Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cn.smallbun.screw.core.engine.word;

import cn.smallbun.screw.core.engine.AbstractTemplateEngine;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.exception.ProduceException;
import cn.smallbun.screw.core.metadata.model.DataModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.FileUtils;
import cn.smallbun.screw.core.util.StringUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;

import static cn.smallbun.screw.core.engine.EngineTemplateType.docx;
import static cn.smallbun.screw.core.util.FileUtils.getFileByPath;

/**
 * word模板
 *
 * @author wangkang
 */
public class WordTemplateEngine extends AbstractTemplateEngine {


    public WordTemplateEngine(EngineConfig templateConfig) {
        super(templateConfig);
    }

    private String templateName;

    {
        try {
            EngineConfig engineConfig = getEngineConfig();
            String path = engineConfig.getCustomTemplate();
            //自定义模板
            if (StringUtils.isNotBlank(path) && FileUtils.isFileExists(path)) {
                //获取父目录
                templateName = Objects.requireNonNull(getFileByPath(path)).getParent();
            } else {
                templateName =
                        engineConfig.getProduceType().getTemplateDir() + engineConfig.getFileType().getTemplateNamePrefix() + docx.getSuffix();
            }
        } catch (Exception e) {
            throw ExceptionUtils.mpe(e);
        }
    }

    /**
     * 生成文档
     *
     * @param info {@link DataModel}
     * @throws ProduceException ProduceException
     */
    @Override
    public void produce(DataModel info, String docName) throws ProduceException {
        Assert.notNull(info, "DataModel can not be empty!");
        File file = getFile(docName);
        try {
            LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
            Configure config = Configure.builder().bind("columns", policy).bind("tables", policy).build();
            XWPFTemplate compile = XWPFTemplate.compile(WordTemplateEngine.class.getResourceAsStream(templateName),
                    config);
            XWPFTemplate template = compile.render(info);
            template.writeAndClose(new FileOutputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
